<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>试卷编辑详情页</title>
<link rel="stylesheet" type="text/css" href="/queen/Public/Home/css/global.css" />
<link rel="stylesheet" type="text/css" href="/queen/Public/Home/css/manage.css" />
<script src="/queen/Public/Static/jquery-1.9.1.min.js"></script>
<body class="global_form global_table global_icon global_formList">
<div class="global_head"></div>
<div class="global_width survey_details">
  <div class="content"> 
    
    <div class="box_title" id="show_title">
      <input type="hidden" name="paper_id" id="paper_id" value="<?php echo ($info['paper_info']['id']); ?>" />
      <input type="text" name="paper_id" id="paper_title" value="<?php echo ($info['paper_info']['name']); ?>" placeholder="试卷名称"/>
      <textarea rows="" cols="" name="description" id="description" placeholder="试卷描述"><?php echo ($info['paper_info']['description']); ?></textarea>
    </div>
   
    <!-- 编辑完成后的显示状态-->
    <div class="box_title end" style="display:none">
      <h2><?php echo ($info['paper_info']['name']); ?></h2>
      <p><?php echo ($info['paper_info']['description']); ?></p>
    </div>
    <div class="details" id="details">
    <div id="tem">
	    <?php if(count($info['subject_info']) > 0): if(is_array($info['subject_info'])): foreach($info['subject_info'] as $key=>$subInfo): ?><div class="question" type="<?php echo ($subInfo['type']); ?>">
			        <h3> <?php if($subInfo['type'] == 1): ?>单选题目： <?php else: ?> 多选题目：<?php endif; ?>
			        	<input type="text" name="title" value="<?php echo ($subInfo['title']); ?>" id="<?php echo ($subInfo['id']); ?>"> 分数：<input type="text" name="score" value="<?php echo ($subInfo['score']); ?>">
			        </h3>
			        <div class="option">
			        <?php if(count($subInfo['field']) > 0): foreach($subInfo['field'] as $v) { ?>
				          <p>
				          <?php if($subInfo['type'] == 1): if($v['correct'] == 1): ?><input type="radio" name="correct" checked/>
					          <?php else: ?>
					          	<input type="radio" name="correct" /><?php endif; ?>
				          <?php else: ?>
				              <?php if($v['correct'] == 1): ?><input type="checkbox" checked/>
                              <?php else: ?>
                                <input type="checkbox" /><?php endif; endif; ?>
				            &nbsp;&nbsp;<input type="text" name="field" value="<?php echo ($v['title']); ?>" id="<?php echo ($v['id']); ?>" />
				            </p>
				            <?php } endif; ?>
			        	</div>
			        </div><?php endforeach; endif; endif; ?>
     </div>
      <div class="choose"> <a href="javascript:void(0);" class="btn" onclick="addTem(1);">单选题</a> <a href="javascript:void(0);" class="btn" onclick="addTem(2);">多选题</a></div>
      <div class="save"> <a class="first" href="javascript:void(0);" onclick="savePaper();">保存表单</a></div>
    </div>
  </div>
</div>
</body>
<script>
function addTem(type) {
	var html = '';
	if (type == 1) {
		html += '<div class="question" type="1">\
        <h3>单选题目：<input type="text" name="title"> 分数：<input type="text" name="score"></h3>\
        <div class="option">\
          <p>\
            <input type="radio" name="correct" />\
            &nbsp;&nbsp;<input type="text" name="field" />\
            </p>\
          <p>\
            <input type="radio" name="correct" checked="checked" />\
            &nbsp;&nbsp;<input type="text" name="field" /></p>\
          <p>\
            <input type="radio" name="correct" />\
            &nbsp;&nbsp;<input type="text" name="field" /></p>\
          <p>\
            <input type="radio" name="correct" />\
            &nbsp;&nbsp;<input type="text" name="field" /></p>\
        </div>\
      </div>';
	} else if (type == 2) {
		html += '<div class="question" type="2">\
	        <h3>多选题目：<input type="text" name="title"> 分数：<input type="text" name="score"></h3>\
	        <div class="option">\
	          <p>\
	            <input type="checkbox" />\
	            &nbsp;&nbsp;<input type="text" name="field"  /><br />\
	          </p>\
	          <p>\
	            <input type="checkbox"  />\
	            &nbsp;&nbsp;<input type="text" name="field"  /><br />\
	          </p>\
	          <p>\
	            <input type="checkbox"  />\
	            &nbsp;&nbsp;<input type="text" name="field"  /><br />\
	          </p>\
	          <p>\
	            <input type="checkbox"  />\
	            &nbsp;&nbsp;<input type="text" name="field"  /><br />\
	          </p>\
	        </div>\
	      </div>';
	}
	$("#tem").append(html);
}

function savePaper() {
	//var submitData = {};
	var paper_id = $("#paper_id").val();
	var paper_title = $("#paper_title").val();
	var description = $("#description").val();
	var list = new Array();
	var submit = true;
	if (paper_title == '') {
		alert("试卷名称不能为空");
		return false;
	}
	$("div.question").each(function(index,record) {
		var title = $(this).find("input[name='title']").val();
		var title_id = $(this).find("input[name='title']").attr('id');
		var score = $(this).find("input[name='score']").val();
		var type = $(this).attr('type');
		var array = new Array();
		if (title == '') {
			alert("题目不能为空");
			submit = false;
			return false;
		} else if (score == '') {
			alert("分数不能为空");
			submit = false;
			return false;
		}
		list[index] = {'title_id':title_id,'title':title,'score':score,'type':type,'field':[]};
		$(this).find("p").each(function(idx,rec){
			var field = $(this).children("input[name='field']").val();
			var field_id = $(this).children("input[name='field']").attr('id');
			var correct = 0;
			if ($(this).children("input").eq(0).is(":checked")) {
				correct = 1;
			}
			array[idx] = {'field_id':field_id,'correct':correct,'content':field};
		});
		list[index].field = array;
	});
	if (submit) {
		$.post("<?php echo U('savePaper');?>",{paper_id:paper_id,paper_title:paper_title,description:description,data:list},function(msg){
			if (msg == 1) {
				alert("保存成功");
				location.href="<?php echo U('paperList');?>";
			} else {
				alert("保存失败");
			}
		});
	}
}


</script>

</html>